package com.example.daily

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.navigation.compose.rememberNavController
import com.example.daily.data.UserPreferences
import com.example.daily.ui.navigation.NavGraph
import com.example.daily.ui.navigation.Screen
import com.example.daily.ui.theme.DailyTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

    @Inject
    lateinit var userPreferences: UserPreferences

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navController = rememberNavController()
            val userId by userPreferences.getUserId().collectAsState(initial = null)
            
            Log.d("MainActivity", "Current userId: $userId")
            
            val startDestination = if (userId != null) {
                Log.d("MainActivity", "User is logged in, navigating to Home")
                Screen.Home.route
            } else {
                Log.d("MainActivity", "User is not logged in, navigating to Login")
                Screen.Login.route
            }

            DailyTheme {
                NavGraph(
                    navController = navController,
                    startDestination = startDestination,
                    onAuthSuccess = {
                        navController.navigate(Screen.Home.route) {
                            popUpTo(Screen.Login.route) { inclusive = true }
                        }
                    }
                )
            }
        }
    }
}